from fastapi import APIRouter, HTTPException, Depends
from api.package_material.basic import (
    PackageMaterial,
    get_materials,
    add_material,
    update_material,
    delete_material
)
from util.JWT import get_current_user

router = APIRouter(
    prefix="/package-material/basic",
    tags=["package-material"]
)

@router.get("/list")
async def get_material_list(search: str = '', page: int = 1, page_size: int = 10, id: dict = Depends(get_current_user)):
    result = get_materials(search, page, page_size)
    if result is None:
        raise HTTPException(status_code=500, detail="获取包材列表失败")
    return {"code": 200, "data": result}

@router.post("/add")
async def create_material(material: PackageMaterial, id: dict = Depends(get_current_user)):
    result = add_material(material)
    if result is None:
        raise HTTPException(status_code=500, detail="新增包材失败")
    return {"code": 200, "message": "新增成功"}

@router.put("/update")
async def modify_material(material: PackageMaterial, id: dict = Depends(get_current_user)):
    result = update_material(material)
    if result is None:
        raise HTTPException(status_code=500, detail="更新包材失败")
    return {"code": 200, "message": "更新成功"}

@router.delete("/delete/{code}")
async def remove_material(code: str, operator: str = 'system', id: dict = Depends(get_current_user)):
    result = delete_material(code, operator)
    if result is None:
        raise HTTPException(status_code=500, detail="删除包材失败")
    return {"code": 200, "message": "删除成功"} 